<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Interactive Showroom</title>
    <style>
        :root {
            --bg: #f4f6f9;
            --text: #333;
            --card: white;
            --header: linear-gradient(to right, #4facfe, #00f2fe);
            --shadow: rgba(0, 0, 0, 0.05);
        }

        body.dark {
            --bg: #121212;
            --text: #ddd;
            --card: #1e1e1e;
            --header: linear-gradient(to right, #222, #444);
            --shadow: rgba(255, 255, 255, 0.05);
        }

        body {
            font-family: 'Segoe UI', sans-serif;
            margin: 0;
            padding: 0;
            background: var(--bg);
            color: var(--text);
            transition: background 0.3s, color 0.3s;
        }

        header {
            background: var(--header);
            color: white;
            padding: 2rem;
            text-align: center;
        }

        section {
            padding: 2rem;
            margin: 1rem auto;
            max-width: 800px;
            background: var(--card);
            border-radius: 10px;
            box-shadow: 0 4px 12px var(--shadow);
        }

        h2 { margin-top: 0; }

        .fade-in-up {
            opacity: 0;
            transform: translateY(20px);
            transition: opacity 0.8s ease, transform 0.8s ease;
        }

        .fade-in-up.visible {
            opacity: 1;
            transform: translateY(0);
        }

        input, select, textarea {
            padding: 0.5rem;
            margin: 0.5rem 0;
            width: 100%;
            border: 1px solid #ccc;
            border-radius: 6px;
            transition: box-shadow 0.3s, border 0.3s;
            background: var(--card);
            color: var(--text);
        }

        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #007BFF;
            box-shadow: 0 0 8px rgba(0,123,255,0.2);
        }

        button {
            padding: 0.6rem 1.2rem;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: transform 0.2s ease;
        }

        button:active { transform: scale(0.95); }

        label { display: block; margin-top: 1rem; }

        .toggle {
            position: relative;
            display: inline-block;
            width: 50px;
            height: 26px;
        }

        .toggle input { opacity: 0; width: 0; height: 0; }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0; left: 0;
            right: 0; bottom: 0;
            background-color: #ccc;
            border-radius: 26px;
            transition: background-color 0.3s;
        }

        .slider::before {
            content: "";
            position: absolute;
            height: 22px; width: 22px;
            left: 2px; bottom: 2px;
            background-color: white;
            border-radius: 50%;
            transition: transform 0.3s;
        }

        .toggle input:checked + .slider {
            background-color: #4caf50;
        }

        .toggle input:checked + .slider::before {
            transform: translateX(24px);
        }

        .draggable-list {
            list-style: none;
            padding: 0;
        }

        .draggable-list li {
            background: #e0e0e0;
            padding: 10px 15px;
            margin: 8px 0;
            border-radius: 6px;
            cursor: move;
            user-select: none;
            transition: background 0.2s;
        }

        .draggable-list li.dragging {
            opacity: 0.5;
        }

        footer {
            text-align: center;
            padding: 1rem;
            font-size: 0.9rem;
            color: #777;
        }
    </style>
</head>
<body>

<header>
    <h1>🎨 Interactive Showroom - VisionUtils 测试（请打开控制台查看结果）</h1>
    <p>Now with Drag & Drop and Dark Mode</p>
</header>

<section class="fade-in-up">
    <h2>📝 User Information</h2>
    <label>Name: <input type="text" placeholder="Enter your name" /></label>
    <label>Email: <input type="email" placeholder="Enter your email" /></label>
    <label>Comment: <textarea placeholder="Leave your message"></textarea></label>
    <button onclick="alert('Form Submitted!')">Submit</button>
</section>

<section class="fade-in-up">
    <h2>🌓 Appearance</h2>
    <label>
        Dark Mode:
        <span class="toggle">
        <input type="checkbox" id="darkToggle">
        <span class="slider"></span>
      </span>
    </label>
</section>

<section class="fade-in-up">
    <h2>📋 Drag & Drop List</h2>
    <p>Reorder the items below:</p>
    <ul class="draggable-list" id="sortableList">
        <li draggable="true">🟢 Task A</li>
        <li draggable="true">🔵 Task B</li>
        <li draggable="true">🟣 Task C</li>
        <li draggable="true">🟠 Task D</li>
    </ul>
</section>

<footer>© 2025 Interactive Showroom by ChatGPT</footer>

<script>
    // Animate on scroll
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) entry.target.classList.add('visible');
        });
    }, { threshold: 0.1 });

    document.querySelectorAll('.fade-in-up').forEach(el => observer.observe(el));

    // Dark mode toggle
    document.getElementById('darkToggle').addEventListener('change', (e) => {
        document.body.classList.toggle('dark', e.target.checked);
    });

    // Drag and drop
    const list = document.getElementById('sortableList');
    let draggingEl;

    list.addEventListener('dragstart', (e) => {
        draggingEl = e.target;
        e.target.classList.add('dragging');
    });

    list.addEventListener('dragend', (e) => {
        e.target.classList.remove('dragging');
        draggingEl = null;
    });

    list.addEventListener('dragover', (e) => {
        e.preventDefault();
        const afterEl = getDragAfterElement(list, e.clientY);
        if (afterEl == null) {
            list.appendChild(draggingEl);
        } else {
            list.insertBefore(draggingEl, afterEl);
        }
    });

    function getDragAfterElement(container, y) {
        const els = [...container.querySelectorAll('li:not(.dragging)')];
        return els.reduce((closest, child) => {
            const box = child.getBoundingClientRect();
            const offset = y - box.top - box.height / 2;
            if (offset < 0 && offset > closest.offset) {
                return { offset: offset, element: child };
            } else {
                return closest;
            }
        }, { offset: Number.NEGATIVE_INFINITY }).element;
    }
</script>
</body>
<script src="vision_utils_test.js"></script> <!-- 路径视情况调整 -->
<script>
    (function analyzeAllTextElementsContrast() {
        const VisionUtils = window.VisionUtils;

        // 辅助函数：判断是否是纯文本节点或有文本内容的元素
        function hasVisibleText(el) {
            if (el.nodeType === Node.TEXT_NODE && el.textContent.trim()) return true;
            if (el.nodeType !== Node.ELEMENT_NODE) return false;
            return el.textContent.trim().length > 0 && window.getComputedStyle(el).display !== 'none';
        }

        // 主逻辑：遍历 DOM 并分析每个有文本的元素
        function analyzeContrastForAllTextElements() {
            const walker = document.createTreeWalker(
                document.body,
                NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
                {
                    acceptNode: node => {
                        return hasVisibleText(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
                    }
                }
            );

            let node;
            while ((node = walker.nextNode())) {
                if (node.nodeType === Node.TEXT_NODE) {
                    const parentEl = node.parentElement;
                    if (!parentEl) continue;

                    const foreground = parseRGB(window.getComputedStyle(parentEl).color);
                    const background = VisionUtils.getEffectiveBgColor(parentEl);

                    if (foreground && background) {
                        const result = VisionUtils.analyzeContrast(foreground, background);
                        logResult(`📄 文本：“${node.textContent.trim()}”`, foreground, background, result);
                    }
                } else if (node.nodeType === Node.ELEMENT_NODE) {
                    const textContent = node.textContent.trim();
                    if (!textContent) continue;

                    const foreground = parseRGB(window.getComputedStyle(node).color);
                    const background = VisionUtils.getEffectiveBgColor(node);

                    if (foreground && background) {
                        const result = VisionUtils.analyzeContrast(foreground, background);
                        logResult(`📄 元素：<${node.tagName.toLowerCase()}> "${textContent}"`, foreground, background, result);

                        // 可选：对不达标的文本添加高亮样式
                        if (result.level.includes("❌")) {
                            node.style.backgroundColor = "#ffdddd";
                            node.style.border = "1px solid red";
                            node.style.padding = "2px";
                        }
                    }
                }
            }
        }

        // 格式化输出结果到控制台
        function logResult(label, foreground, background, result) {
            // console.groupCollapsed(label);
            console.log('前景色:', foreground);
            console.log('背景色:', background);
            console.log('对比度:', result);
            // console.groupEnd();
        }

        // 支持 CSS 颜色字符串解析为 RGB
        function parseRGB(colorStr) {
            const match = colorStr.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/i);
            if (!match) return null;
            return {
                r: parseInt(match[1]),
                g: parseInt(match[2]),
                b: parseInt(match[3]),
                a: 1
            };
        }

        analyzeContrastForAllTextElements();
    })();
</script>

<script>
</script>

</html>
